local file severity_balance //EDIT

capture log close
log using "$logs/`file'_$S_DATE.log", replace






//load analysis sample, but also include E&M Code
use patient provnpi* encdate cpt_1 using "$data_in/ED_CPT", clear
replace encdate = dofc(encdate)
drop if mi(provnpi1)
bys patient encdate provnpi: keep if _n ==_N
rename patient pid_pde
merge 1:1 pid_pde encdate provnpi using "$data_out/provider_propensity", keep(2 3)  nogen



//Use ICD9 and 10 injury codes plus ICU E&M

gen ICU = cpt_1 == "99291"
drop if encdate >= d(01oct2015)
 

 
 //go through and figure out which groups are driving results. Appears to be open wounds. 
 
 

gen inj_fracture = inrange(dx1, "800", "829")
gen inj_dislocation = inrange(dx1, "830", "839")
gen inj_sprains_strains = inrange(dx1, "840", "848")
gen inj_intracranial = inrange(dx1, "850", "854")
gen inj_internal = inrange(dx1, "860", "869")
gen inj_open_wound = inrange(dx1, "870", "887") | inrange(dx1, "890", "897")
gen inj_blood_vessels = inrange(dx1, "900", "904")
gen inj_contusion_superficial = inrange(dx1, "910", "924")
gen inj_crushing = inrange(dx1, "925", "929")
gen inj_foreign_body = inrange(dx1, "930", "939")
gen inj_burns = inrange(dx1, "940", "949")
gen inj_spinal = inrange(dx1, "950", "957")



*top 10 diagnoses
cap drop N
cap drop n
bys diag: gen N = _N
bys diag: gen n =1 if _n ==1

bys diag: egen mean_opioid = mean(ed_prescription)
preserve
keep if n ==1
drop if N <100
sort mean_opioid
gen num = _n
summ mean_opioid, d
list num temp_dx3 mean_opioid N, noobs clean
keep diag temp_dx3 num N mean_opioid
save "$data_out/diagnosis_opioid_rates", replace
restore

cap drop temp_diag*

tab temp_dx if inlist(temp_dx3,"787", "786", "789", "724", "465", "462", "845" ,"780", "847") | temp_dx3 == "V68", gen(common_diag) 


*top 10 median prescribed opiates

tab temp_dx if inlist(temp_dx3,"917", "V67", "870", "380", "873", "288", "457" ,"686", "616") | temp_dx3 == "423", gen(median_diag) 



*10 inframarginal prescribed
cap drop temp_diag*

tab temp_dx if inlist(temp_dx3,"415", "998", "834", "717", "949", "718", "724" ,"274", "846") | temp_dx3 == "942", gen(marginal_diag) 



foreach var of varlist  common_diag* median_diag* marginal_diag* {
recode `var' .=0
}


//NYU Algorithm with 100% certain diagnoses

rename dx1 discharge_dx
merge m:1 discharge_dx using "$lookup/NYU_0615", keep(1 3) gen(_m_nyu)



gen emergency = ed_care_needed__not_preventable_ == 1
replace emergency = 1 if ed_care_needed__preventable_avoi == 1 | emergent__pc_treatable ==1
gen non_emergency = non_emergent == 1


cap postclose balance
postfile balance str16 Variable Mean Coefficient Std_Error p_value   using "$plotdir/balance_diagnoses_propensity.dta", replace

foreach dx of varlist ICU inj_* common_diag* median_diag* marginal_diag*   emergency non_emergency alcohol drug  {
eststo balance: reghdfe `dx' propensity, absorb(hym hd) cluster(provID)
estadd ysumm, replace
post balance ("`dx'") (e(ymean)) (_b[propensity]) (_se[propensity]) (2*normal(-abs(_b[propensity]/_se[propensity])))

}
postclose balance

postfile balance str16 Variable Mean Coefficient Std_Error p_value   using "$plotdir/balance_diagnoses_binary.dta", replace

foreach dx of varlist ICU inj_* common_diag* median_diag* marginal_diag*   emergency non_emergency alcohol drug  {
eststo balance: reghdfe `dx' above if inlist(quartile,1,4), absorb(hym hd) cluster(provID)
estadd ysumm, replace
post balance ("`dx'") (e(ymean)) (_b[above]) (_se[above]) (2*normal(-abs(_b[above]/_se[above])))

}
postclose balance


cap postclose balance
postfile balance str16 Variable Mean Coefficient Std_Error p_value   using "$plotdir/balance_diagnoses_propensityC.dta", replace

foreach dx of varlist ICU inj_* common_diag* median_diag* marginal_diag*   emergency non_emergency alcohol drug  {
eststo balance: reghdfe `dx' propensity i.age_bin race_white female junior_enlisted college married longevity afqt_p, absorb(hym hd mos) cluster(provID)
estadd ysumm, replace
post balance ("`dx'") (e(ymean)) (_b[propensity]) (_se[propensity]) (2*normal(-abs(_b[propensity]/_se[propensity])))

}
postclose balance

postfile balance str16 Variable Mean Coefficient Std_Error p_value   using "$plotdir/balance_diagnoses_binaryC.dta", replace

foreach dx of varlist ICU inj_* common_diag* median_diag* marginal_diag*   emergency non_emergency alcohol drug  {
eststo balance: reghdfe `dx' above i.age_bin race_white female junior_enlisted college married longevity afqt_p if inlist(quartile,1,4), absorb(hym hd mos) cluster(provID)
estadd ysumm, replace
post balance ("`dx'") (e(ymean)) (_b[above]) (_se[above]) (2*normal(-abs(_b[above]/_se[above])))

}
postclose balance






	
//table
use "$plotdir/balance_diagnoses_propensity.dta", clear
rename ( Coefficient Std_Error p_value) (Continuous_Coef Continuous_SE Continuous_P)
merge 1:1 Variable using "$plotdir/balance_diagnoses_binary.dta", nogen 
rename ( Coefficient Std_Error p_value) (Binary_Coef Binary_SE Binary_P)

export excel using "\\facaa7pde01p\PDE\Projects\ACADEMIC\ACADEMIC-UOA\Draft - July 2021\Revisions - AEJ Applied\Tables and Figures\Diagnosis_Balance.xls", sheetreplace firstrow(variables)



use "$plotdir/balance_diagnoses_propensityC.dta", clear
rename ( Coefficient Std_Error p_value) (Continuous_Coef Continuous_SE Continuous_P)
merge 1:1 Variable using "$plotdir/balance_diagnoses_binaryC.dta", nogen 
rename ( Coefficient Std_Error p_value) (Binary_Coef Binary_SE Binary_P)

export excel using "\\facaa7pde01p\PDE\Projects\ACADEMIC\ACADEMIC-UOA\Draft - July 2021\Revisions - AEJ Applied\Tables and Figures\Diagnosis_Balance_Controls.xls", sheetreplace firstrow(variables)









